package tm;

import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;

/**
 * 事务管理
 * 事务前后需是同一个线程内部的链接Connection
 * @author wsz
 * @date 2021/10/13 11:18
 **/
@Slf4j
public class TransactionManager {

    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    public void beginTransaction() {
        try {
            log.info("beginTransaction");
            connectionUtils.getConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void commit() {
        log.info("commit");
        try {
            connectionUtils.getConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void rollback() {
        log.info("rollback");
        try {
            connectionUtils.getConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
